package com.hl233.framework.service.impl;

import com.hl233.framework.common.Constants;
import com.hl233.framework.pojo.LoginUser;
import com.hl233.framework.pojo.User;
import com.hl233.framework.service.LoginService;
import com.hl233.framework.utils.JwtUtil;
import com.hl233.framework.utils.RedisUtils;
import com.hl233.framework.utils.Result;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author 张振强15065206763
 * @create 2024-09-14
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Result login(User user) {
        //AuthenticationManager进行用户认证
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getLoginName(),user.getPassword());
        Authentication authentication = authenticationManager.authenticate(authenticationToken);
        //认证没有通过给出对应的提示
        if (Objects.isNull(authentication)){
            throw new RuntimeException("登录失败");
        }
        //认证通过 使用userid生成jwt 返回jwt
        LoginUser loginUser = (LoginUser)authentication.getPrincipal();
        Integer userId = loginUser.getUser().getUserId();
        System.out.println("拿到loginuser"+loginUser.getUser().getUserId());
        System.out.println("拿到loginuser"+loginUser.getUser());

        // Jwt存储信息
        Map<String, Object> claimsMap = new HashMap<String, Object>();
        claimsMap.put(Constants.DETAILS_USER_ID, userId);
        claimsMap.put(Constants.DETAILS_USERNAME, user.getLoginName());
        String jwtToken = JwtUtil.createToken(claimsMap);
        //把完整用户信息存入redis userid作为key
        redisUtils.setCacheObject("loginUser:"+userId,loginUser);


        return Result.ok().data("token", jwtToken);
    }

    @Override
    public Result logout() {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken)SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser)authentication.getPrincipal();
        Integer userId = loginUser.getUser().getUserId();
        System.out.println("拿到的userId"+userId);
        redisUtils.deleteObject("loginUser:"+userId);
        return Result.ok().message("注销成功");
    }

    @Override
    public Result getInfo() {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken)SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser)authentication.getPrincipal();
        System.out.println("这个是什么"+loginUser);
        Integer userId = loginUser.getUser().getUserId();
        LoginUser loginUserInfo = redisUtils.getCacheObject("loginUser:" + userId);
        return Result.ok().data("info",loginUserInfo);
    }
}
